#include <windows.h>
#include <doclist.h>

#include "BrowseForFolderDialog.h"
#include "DamDialog.h"
#include "Resources.h"
#include "StartupDialog.h"

StartupDialog::StartupDialog(HINSTANCE hInstance, InputDevice *inDevice, OutputDevice *outDevice, const MusicTime &latency) throw(): Dialog(hInstance, TEXT("Digital Audio Mobile"), CS_CLOSE), m_dlg(0), m_inDev(inDevice), m_outDev(outDevice), m_latency(latency) {
    DOCLISTCREATE dlc;
    dlc.dwStructSize = sizeof(DOCLISTCREATE);
    dlc.hwndParent = hWnd();
    dlc.pszFolder = 0;
    dlc.pstrFilter = TEXT("DAM projects\0*.damproj\0");
    dlc.wFilterIndex = 0;
    dlc.wId = 1000;
    dlc.dwFlags = 0;
    m_hFiles = DocList_Create(&dlc);
    DocList_Refresh(m_hFiles);

    RECT rect;
    GetClientRect(hWnd(), &rect);
    SetWindowPos(m_hFiles, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_DRAWFRAME);

    TBBUTTONINFO bi;
    ZeroMemory(&bi, sizeof(TBBUTTONINFO));
    bi.cbSize = sizeof(TBBUTTONINFO);
    bi.dwMask = TBIF_TEXT | TBIF_STATE;
    bi.pszText = TEXT("New");
    bi.fsState = TBSTATE_ENABLED;
    SendMessage(hMenuBar(), TB_SETBUTTONINFO, ID_BUTTON, LPARAM(&bi));
}

StartupDialog::~StartupDialog() {
    DestroyWindow(m_hFiles);
}

DamDialog *StartupDialog::openedDialog() throw() {
    return(m_dlg);
}

bool StartupDialog::close(CloseCommand cc) throw() {
    return(true);
}

void StartupDialog::controlNotification(HWND hWnd, WPARAM notification) throw() {
    if(hWnd == m_hFiles) {
        switch(notification) {
            case DLN_DELETINGSEL:{
                // Delete the audio folder as well.
                TCHAR path[MAX_PATH];
                DocList_GetSelectedPathname(m_hFiles, path, MAX_PATH);
                for(int i = _tcslen(path); path[i] != '.'; i--) {
                    path[i] = TEXT('\0');
                }
                _tcscat(path, TEXT("Audio"));
                TCHAR findPath[MAX_PATH];
                _tcscpy(findPath, path);
                _tcscat(findPath, TEXT("\\"));
                _tcscat(findPath, TEXT("*.wav"));
                WIN32_FIND_DATA fd;
                HANDLE hFind = FindFirstFile(findPath, &fd);
                if(hFind != INVALID_HANDLE_VALUE) {
                    do {
                        TCHAR filePath[MAX_PATH];
                        _tcscpy(filePath, path);
                        _tcscat(filePath, TEXT("\\"));
                        _tcscat(filePath, fd.cFileName);
                        DeleteFile(filePath);
                    }
                    while(FindNextFile(hFind, &fd));
                }
                RemoveDirectory(path);
                break;
            }
            case DLN_ITEMACTIVATED:{
                // Open the selected project.
                TCHAR path[MAX_PATH];
                DocList_GetSelectedPathname(m_hFiles, path, MAX_PATH);
                SetCursor(LoadCursor(0, IDC_WAIT));
                m_dlg = new DamDialog(hInstance(), path, m_inDev, m_outDev, m_latency);
                postClose(CC_OK);
                SetCursor(0);
                break;
            }
        }
    }
}

void StartupDialog::menuNotification(unsigned int itemId) throw() {
    if(itemId == ID_BUTTON) {
        BrowseForFolderDialog browseDlg(hInstance(), TEXT("New song"));
        if(browseDlg.open() == CC_OK) {
            CreateDirectory(browseDlg.selectedPath(), 0);

            TCHAR file[MAX_PATH];
            _tcscpy(file, browseDlg.selectedPath());
            _tcscat(file, TEXT("\\"));
            _tcscat(file, browseDlg.selectedFile());
            _tcscat(file, TEXT(".damproj"));
            m_dlg = new DamDialog(hInstance(), file, m_inDev, m_outDev, m_latency);
            postClose(CC_OK);
        }
    }
}
